package com.iwdnb.gkgz.application.utils;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

import com.iwdnb.gkgz.common.model.dto.StockDayData;
import com.iwdnb.gkgz.common.utils.BigDecimalUtils;
import com.iwdnb.gkgz.common.utils.StockUtils;
import com.iwdnb.gkgz.infrastructure.dao.repo.StockDayDataRepository;
import org.apache.commons.collections4.CollectionUtils;

import static com.iwdnb.gkgz.common.constant.GkgzConstants.RED_GREEN_SIGNAL_CODE;

public class RedGreenLightUtils {

    private static StockDayDataRepository stockDayDataRepository;

    private static List<StockDayData> stockDayDataList;

    public RedGreenLightUtils(StockDayDataRepository stockDayDataRepository) {
        RedGreenLightUtils.stockDayDataRepository = stockDayDataRepository;
        loadData();
    }

    public static boolean isAvailable(String date) {
        if (CollectionUtils.isEmpty(stockDayDataList)) {
            return false;
        }
        StockDayData data = StockUtils.getStockDayData(stockDayDataList, date);
        if (Objects.isNull(data)) {
            return false;
        }
        return BigDecimalUtils.isGt(data.getRate(), BigDecimal.ZERO);
    }


    private void loadData() {
        stockDayDataList = stockDayDataRepository.queryAll(RED_GREEN_SIGNAL_CODE);
    }
}
